

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public class TestClientsXL {

	static WebDriver myD;
	static String qFirstName, qLastName, qCompanyName, qEmail, qPhone,
			qCountry, qBrowser, qRemote, qOnsite, qURL;
	static String actResults, expResults;
	int xRows = 0, xCols = 0;
	String[][] qTD;

	private WebDriver firefoxDriver = null;
	private WebDriver chromeDriver = null;
	private WebDriver ieDriver = null;

	@Before
	public void mainBefore() throws Exception {
		// Define you webdriver and initialize it
		qTD = readXL(
				"C:\\Users\\Nilam\\Selenium_live_project\\ClientDataRead.xls",
				"Sheet1");
		System.setProperty("webdriver.chrome.driver",
				"c:\\drivers\\chromedriver.exe");
		System.setProperty("webdriver.ie.driver",
				"c:\\drivers\\IEDriverServer.exe");
	}

	@Test
	public void mainTest() throws Exception {
		String qResult1, qResult2, qResult3, qError, qScreenShot;
		// Get each set of Test Data
		System.out.println("Trying to execute after reading XL");

		// Initialise additional columns

		qTD[0][11] = "Status";
		qTD[0][12] = "Message";
		for (int vRow = 1; vRow < xRows; vRow++) {
			// Initialive the variables
			System.out
					.println("Executing test for test data : " + qTD[vRow][0]);
			qFirstName = qTD[vRow][1];
			qLastName = qTD[vRow][2];
			qCompanyName = qTD[vRow][3];
			qEmail = qTD[vRow][4];
			qPhone = qTD[vRow][5];
			qCountry = qTD[vRow][6];
			qBrowser = qTD[vRow][7];
			qRemote = qTD[vRow][8];
			qOnsite = qTD[vRow][9];
			qURL = qTD[vRow][10];
			qResult2 = "You are all set";

			boolean bRemote = qRemote.equals("Enabled") ? true : false;
			boolean bOnsite = qOnsite.equals("Enabled") ? true : false;

			// Execute the Test Case for each set of Test Data
			// TC001 - Search for Clients page
			System.out.println("Opening url : " + qURL + " in :" + qBrowser);

			try {

				if (qBrowser.equals("FF")) {
					if (firefoxDriver == null) {
						firefoxDriver = new FirefoxDriver();
					}
					myD = firefoxDriver;

				}
				if (qBrowser.equals("IE")) {
					if (ieDriver == null) {
						ieDriver = new InternetExplorerDriver();
					}
					myD = ieDriver;
				}
				if (qBrowser.equals("HTML")) {
					myD = new HtmlUnitDriver();
				}
				if (qBrowser.equals("Chrome")) {
					if (chromeDriver == null) {
						chromeDriver = new ChromeDriver();
					}
					myD = chromeDriver;
				}

				myD.navigate().to(qURL);

				myD.findElement(By.id("inf_field_FirstName")).sendKeys(
						qFirstName);
				myD.findElement(By.id("inf_field_LastName")).clear();
				myD.findElement(By.id("inf_field_LastName"))
						.sendKeys(qLastName);
				myD.findElement(By.id("inf_field_Company")).clear();
				myD.findElement(By.id("inf_field_Company")).sendKeys(
						qCompanyName);
				myD.findElement(By.id("inf_field_Email")).clear();
				myD.findElement(By.id("inf_field_Email")).sendKeys(qEmail);
				myD.findElement(By.id("inf_field_Phone1")).clear();
				myD.findElement(By.id("inf_field_Phone1")).sendKeys(qPhone);
				myD.findElement(By.id("inf_custom_Country0")).clear();
				myD.findElement(By.id("inf_custom_Country0"))
						.sendKeys(qCountry);
				myD.findElement(By.id("inf_custom_Jobrequirements")).clear();
				myD.findElement(By.id("inf_custom_Jobrequirements"))
						.sendKeys(
								"This is just the testing purpose . Please neglect the mail");

				if (bRemote && bOnsite) {
					myD.findElement(By.id("inf_option_OnsiteorRemote_1398"))
							.click();
				} else {
					if (bRemote) {
						myD.findElement(By.id("inf_option_OnsiteorRemote_1392"))
								.click();
					} else {
						if (bOnsite) {
							myD.findElement(
									By.id("inf_option_OnsiteorRemote_1394"))
									.click();
						}

					}
				}
				myD.findElement(By.cssSelector("input[type=\"submit\"]"))
						.click();
				Thread.sleep(10000);

				qResult1 = myD
						.findElement(
								By.xpath("//html/body/div[1]/div/div/main/article/header/h1"))
						.getText();

				System.out.println("Results on AUT: " + qResult1);
				System.out.println("Results Expected: " + qResult2);

				if (qResult1.equals(qResult2)) {
					System.out.println("Pass TC001.1");
					qResult1 = "Pass";
				} else {
					System.out.println("Fail TC001.1");
					qResult1 = "Fail";
				}

				qTD[vRow][11] = qResult1;
			} catch (Exception e) {
				qTD[vRow][11] = "Fail";
				qTD[vRow][12] = e.getMessage();
			}

			// myD.close();
		}

		if (firefoxDriver != null)
			firefoxDriver.quit();
		if (ieDriver != null)
			ieDriver.quit();
		if (chromeDriver != null)
			chromeDriver.quit();
	}

	
	public String[][] readXL(String sPath, String iSheet) throws Exception {
		String[][] xData;

		File myxl = new File(sPath);
		FileInputStream myStream = new FileInputStream(myxl);
		HSSFWorkbook myWB = new HSSFWorkbook(myStream);
		HSSFSheet mySheet = myWB.getSheet(iSheet);
		xRows = mySheet.getLastRowNum() + 1;
		xCols = mySheet.getRow(0).getLastCellNum();

		System.out.println("Number of rows : " + xRows);
		System.out.println("Number of columns : " + xCols);
		xData = new String[xRows][xCols + 2];
		for (int i = 0; i < xRows; i++) {
			HSSFRow row = mySheet.getRow(i);
			for (int j = 0; j < xCols; j++) {
				HSSFCell cell = row.getCell(j);
				String value = cellToString(cell);
				xData[i][j] = value;
			}
		}

		myStream.close();

		return xData;
	}

	
	public static String cellToString(HSSFCell cell) {
		// This function will convert an object of type excel cell to a string
		// value
		int type = cell.getCellType();
		Object result;
		switch (type) {
		case HSSFCell.CELL_TYPE_NUMERIC: // 0
			result = cell.getNumericCellValue();
			break;
		case HSSFCell.CELL_TYPE_STRING: // 1
			result = cell.getStringCellValue();
			break;
		case HSSFCell.CELL_TYPE_FORMULA: // 2
			throw new RuntimeException("We can't evaluate formulas in Java");
		case HSSFCell.CELL_TYPE_BLANK: // 3
			result = "%";
			break;
		case HSSFCell.CELL_TYPE_BOOLEAN: // 4
			result = cell.getBooleanCellValue();
			break;
		case HSSFCell.CELL_TYPE_ERROR: // 5
			throw new RuntimeException("This cell has an error");
		default:
			throw new RuntimeException("We don't support this cell type: "
					+ type);
		}
		return result.toString();
	}
	@After
	public void mainAfter() throws Exception {
		// Write back into the excel
		writeXL("C:\\Users\\Nilam\\Selenium_live_project\\ClientDataResults.xls", "Sheet2", qTD);	
		
	}
	// Method to write into an XL
	public void writeXL(String sPath, String iSheet, String[][] xData)
			throws Exception {

		File outFile = new File(sPath);
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet osheet = wb.createSheet(iSheet);
		int xR_TS = xData.length;
		int xC_TS = xData[0].length;
		for (int myrow = 0; myrow < xR_TS; myrow++) {
			HSSFRow row = osheet.createRow(myrow);
			for (int mycol = 0; mycol < xC_TS; mycol++) {
				HSSFCell cell = row.createCell(mycol);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(xData[myrow][mycol]);
			}
			FileOutputStream fOut = new FileOutputStream(outFile);
			wb.write(fOut);
			fOut.flush();
			fOut.close();
		}
	}
}
